// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.linqingying.gitcode.extensions

import com.intellij.openapi.project.Project
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValuesManager

const val PARSE_DELAY = 1000L

fun isGitCodeActionsFile(virtualFile: VirtualFile, project: Project?): Boolean {
  if (project == null) return false
  return PsiManager.getInstance(project).findFile(virtualFile)?.let { isGitCodeActionsFile(it) } == true
}

fun isGitCodeActionsFile(psiFile: PsiFile?): Boolean {
  if (psiFile == null) return false
  return CachedValuesManager.getCachedValue(psiFile) {
    CachedValueProvider.Result.create(
      matchesDefaultFilePath(psiFile, gitcodeActionsFilePattern, gitcodeWorkflowsFilePattern),
      psiFile
    )
  }
}

fun isGitCodeActionFile(psiFile: PsiFile?): Boolean {
  if (psiFile == null) return false
  return CachedValuesManager.getCachedValue(psiFile) {
    CachedValueProvider.Result.create(
      matchesDefaultFilePath(psiFile, gitcodeActionsFilePattern),
      psiFile
    )
  }
}

fun isGitCodeWorkflowFile(psiFile: PsiFile?): Boolean {
  if (psiFile == null) return false
  return CachedValuesManager.getCachedValue(psiFile) {
    CachedValueProvider.Result.create(
      matchesDefaultFilePath(psiFile, gitcodeWorkflowsFilePattern),
      psiFile
    )
  }
}

private fun matchesDefaultFilePath(psiFile: PsiFile, vararg pattern: Regex): Boolean {
  val virtualFile = psiFile.originalFile.virtualFile
  if (virtualFile == null) return false
  return (pattern.any { it.matches(StringUtil.newBombedCharSequence(virtualFile.path, PARSE_DELAY)) })
}

val gitcodeActionsFilePattern =
  Regex("""^.*(/|^)action\.ya?ml${'$'}""")

private val gitcodeWorkflowsFilePattern =
  Regex("""^.*/\.gitcode/workflows/.*\.(ya?ml)${'$'}""")
